package com.guochu.sys.controller;

import com.guochu.common.ResultMap;
import com.guochu.common.annotation.CloseAuth;
import com.guochu.sys.entity.Annex;
import com.guochu.sys.service.AnnexService;
import com.guochu.utils.FastDFSUtil;
import com.guochu.utils.MapUtils;
import com.guochu.utils.Pagination;
import io.micrometer.core.instrument.util.StringUtils;
import org.bytedeco.javacv.FrameGrabber;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 附件表
 * 注意：GroupId是文件分组字段，为了方便用户检索文件而设立(Annex)表控制层
 *
 * @author bunting.wei
 * @since 2021-04-13 07:43:06
 */
@RestController
@RequestMapping("sys/annex")
public class AnnexController {
    /**
     * 服务对象(service层)
     */
    @Resource
    private AnnexService annexService;

    /**
     * 上传文件到FastDFS服务器 魏 此方法不允许修改
     * 单个文件上传
     *
     * @param file
     */
    @RequestMapping(value = "/fastDFSUpload", method = RequestMethod.POST)
    public ResultMap fastDFSUpload(@RequestParam("file") MultipartFile file, @RequestParam("groupId") String groupId) {
        Annex annex = new Annex();
        //调用上传fastdfs工具类
        annex = FastDFSUtil.uploadFastdfs(file, groupId);
        return ResultMap.success().put("item", annex);
    }

    /**
     * 上传视频
     *
     * @param file
     * @return 视频路径和视频封面路径
     */
    @RequestMapping(value = "/fastDFSUploadVideo", method = RequestMethod.POST)
    public ResultMap fastDFSUploadVideo(@RequestParam("file") MultipartFile file) throws FrameGrabber.Exception {
        //调用上传fastdfs工具类
        Map<String, Object> limit = FastDFSUtil.uploadVideo(file);
        return ResultMap.success().put("item", limit);
    }

    /**
     * 上传文件到FastDFS服务器 魏 此方法不允许修改
     * 单个文件上传
     *
     * @param file
     */
    @CloseAuth(value = true)
    @RequestMapping(value = "/fastDFSUploadImg", method = RequestMethod.POST)
    public ResultMap fastDFSUploadImg(@RequestParam("file") MultipartFile file) {
        //调用上传fastdfs工具类
        String filePath = FastDFSUtil.uploadFastdfsImg(file);
        return ResultMap.success().put("filePath", filePath);
    }

    /**
     * 添加单条记录
     *
     * @param annex 实例对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMap add(@RequestBody Annex annex) {
        int result = this.annexService.insert(annex);
        return ResultMap.success().put("id", annex.getId());
    }

    /**
     * 修改单条记录
     *
     * @param annex 实例对象
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultMap update(@RequestBody Annex annex) {
        if (annex.getId() != null) {
            int result = this.annexService.update(annex);
            return ResultMap.success();
        } else {
            return ResultMap.success();
        }
    }

    /**
     * 逻辑删除多条记录
     *
     * <p>前端入参为 逗号相隔的ID字符串
     * 正例：id='ID,ID,ID';
     *
     * @param limit 主键id
     * @return
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public ResultMap remove(@RequestBody Map<String, Object> limit) {
        String ids = MapUtils.getString(limit, "id");
        List<String> idList = Arrays.asList(ids.split(","));
        int result = this.annexService.removeByIds(idList);
        return ResultMap.success();
    }

    /**
     * 通过主键ID查询单条数据
     *
     * @param limit 主键
     * @return 单条数据
     */
    @RequestMapping(value = "/getOne", method = RequestMethod.POST)
    public Annex getOne(@RequestBody Map<String, Object> limit) {
        String id = MapUtils.getString(limit, "id");
        return this.annexService.queryById(id);
    }

    /**
     * 根据条件查询分页数据
     *
     * @param limit 查询条件
     * @return 分页数据
     */
    @RequestMapping(value = "/selectPage", method = RequestMethod.POST)
    public ResultMap selectPage(@RequestBody Map<String, Object> limit) {
        //数据校验
        if (limit.get("currentPage") == null || limit.get("pageSize") == null || StringUtils.isBlank(limit.get("currentPage").toString()) || StringUtils.isBlank(limit.get("pageSize").toString())) {
            return ResultMap.error("");
        }
        // 定义分页参数
        Integer currentPage = MapUtils.getInteger(limit, "currentPage");
        Integer pageSize = MapUtils.getInteger(limit, "pageSize");
        Pagination pagination = new Pagination(currentPage, pageSize);
        //创建接收集合
        List<Annex> result = this.annexService.queryPageByLimit(pagination, limit);
        return ResultMap.success().put("items", result).put("page", pagination);
    }

    /**
     * 根据条件查询全部数据
     *
     * @param limit 查询条件
     * @return 所有数据
     */
    @CloseAuth(value = true)
    @RequestMapping(value = "/selectAll", method = RequestMethod.POST)
    public ResultMap selectAll(@RequestBody Map<String, Object> limit) {
        List<Annex> result = this.annexService.queryAllByLimit(limit);
        return ResultMap.success().put("items", result);
    }
}
